home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / IEditor / Generators / C_lib / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-17  |  4.7 KB  |  187 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #include "DEV_IE:Generators/defs.h"
  15.  
  16. extern struct Library *LibInit   ( __A0 BPTR );
  17. extern struct Library *LibOpen   ( __D0 long, __A0 struct Library * );
  18. extern long            LibClose  ( __A0 struct Library * );
  19. extern long            LibExpunge( __A0 struct Library * );
  20.  
  21. struct Library *LibBase = NULL;
  22.  
  23. long SysBase        = NULL;
  24. long DOSBase        = NULL;
  25. long IntuitionBase  = NULL;
  26. long GfxBase        = NULL;
  27. long ReqToolsBase   = NULL;
  28. long GadToolsBase   = NULL;
  29. BPTR SegList        = 0;
  30.  
  31. /*
  32.  *    The Initialization routine is given only a seglist pointer.  Since
  33.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  34.  *    and return either NULL or the library pointer.  Exec has Forbid()
  35.  *    for us during the call.
  36.  */
  37.  
  38. struct Library *LibInit( __A0 BPTR segment )
  39. {
  40.  
  41.     struct Library *lib = NULL;
  42.  
  43.     static const long Vectors[] = {
  44.  
  45.     (long)ALibOpen,
  46.     (long)ALibClose,
  47.     (long)ALibExpunge,
  48.     (long)ALibReserved,
  49.  
  50.     (long)OpenFiles,
  51.     (long)CloseFiles,
  52.     (long)WriteHeaders,
  53.     (long)WriteVars,
  54.     (long)WriteData,
  55.     (long)WriteStrings,
  56.     (long)WriteChipData,
  57.     (long)WriteCode,
  58.     (long)Config,
  59.     -1
  60.     };
  61.  
  62.     SysBase = *(long *)4;
  63.  
  64.     if( DOSBase = OpenLibrary( "dos.library", 36 )) {
  65.     if( IntuitionBase = OpenLibrary( "intuition.library", 36 )) {
  66.         if( GfxBase = OpenLibrary( "graphics.library", 36 )) {
  67.         if( GadToolsBase = OpenLibrary( "gadtools.library", 36 )) {
  68.             if( ReqToolsBase = OpenLibrary( "reqtools.library", 37 )) {
  69.  
  70.             if( LibBase = lib = MakeLibrary( (APTR)Vectors, NULL, NULL, sizeof(struct Generator), NULL )) {
  71.  
  72.                 lib->lib_Node.ln_Type = NT_LIBRARY;
  73.                 lib->lib_Node.ln_Name = LibName;
  74.                 lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  75.                 lib->lib_Version      = 37;
  76.                 lib->lib_Revision     = 0;
  77.                 lib->lib_IdString     = (APTR)LibId;
  78.  
  79.                 ((struct Generator *)lib)->Ext      = "c";
  80.                 ((struct Generator *)lib)->Pattern  = "#?.c";
  81.  
  82.                 SegList = segment;
  83.  
  84.                 AddLibrary( lib );
  85.             }
  86.  
  87.             }
  88.         }
  89.         }
  90.     }
  91.     }
  92.  
  93.     return( lib );
  94. }
  95.  
  96. /*
  97.  *    Open is given the library pointer and the version request.  Either
  98.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  99.  *    Exec has Forbid() for us during the call.
  100.  */
  101.  
  102. struct Library *LibOpen( __D0 long version, __A0 struct Library *lib )
  103. {
  104.     ++lib->lib_OpenCnt;
  105.  
  106.     lib->lib_Flags &= ~LIBF_DELEXP;
  107.  
  108.     return( lib );
  109. }
  110.  
  111. /*
  112.  *    Close is given the library pointer and the version request.  Be sure
  113.  *    not to decrement the open count if already zero.  If the open count
  114.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  115.  *    and return the seglist.  Otherwise we return NULL.
  116.  *
  117.  *    Note that this routine never sets LIBF_DELEXP on its own.
  118.  *
  119.  *    Exec has Forbid() for us during the call.
  120.  */
  121.  
  122. long LibClose( __A0 struct Library *lib )
  123. {
  124.     if( lib->lib_OpenCnt && --lib->lib_OpenCnt )
  125.     return( NULL );
  126.  
  127.     if( lib->lib_Flags & LIBF_DELEXP )
  128.     return( LibExpunge( lib ));
  129.  
  130.     return( NULL );
  131. }
  132.  
  133. /*
  134.  *    We expunge the library and return the Seglist ONLY if the open count
  135.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  136.  *    flag and return NULL.
  137.  *
  138.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  139.  *    might be called from the memory allocator and thus we CANNOT DO A
  140.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  141.  *
  142.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  143.  *    therefore freeze if we called it ourselves.  As far as I can tell
  144.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  145.  *    below.
  146.  */
  147.  
  148. long LibExpunge( __A0 struct Library *lib )
  149. {
  150.     if( lib->lib_OpenCnt ) {
  151.  
  152.     lib->lib_Flags |= LIBF_DELEXP;
  153.     return( NULL );
  154.     }
  155.  
  156.     Remove( &lib->lib_Node );
  157.  
  158.     FreeMem(( char * )lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize );
  159.  
  160.     if( DOSBase ) {
  161.     CloseLibrary( (struct Library *)DOSBase );
  162.     DOSBase = NULL;
  163.     }
  164.  
  165.     if( IntuitionBase ) {
  166.     CloseLibrary( (struct Library *)IntuitionBase );
  167.     IntuitionBase = NULL;
  168.     }
  169.  
  170.     if( GfxBase ) {
  171.     CloseLibrary( (struct Library *)GfxBase );
  172.     GfxBase = NULL;
  173.     }
  174.  
  175.     if( GadToolsBase ) {
  176.     CloseLibrary( (struct Library *)GadToolsBase );
  177.     GadToolsBase = NULL;
  178.     }
  179.  
  180.     if( ReqToolsBase ) {
  181.     CloseLibrary( (struct Library *)ReqToolsBase );
  182.     ReqToolsBase = NULL;
  183.     }
  184.  
  185.     return(( long )SegList );
  186. }
  187.